home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / inetray / netinfo.c < prev    next >
C/C++ Source or Header  |  1993-08-15  |  3KB  |  96 lines

  1. /*======================================================================
  2.                     N E T I N F O . C 
  3.                     doc: Fri Jan 17 13:35:38 1992
  4.                     dlm: Fri Jul 23 16:56:22 1993
  5.                     (c) 1992 ant@julia
  6.                     uE-Info: 20 19 T 0 0 72 2 2 8 ofnI
  7. ======================================================================*/
  8.  
  9. /*
  10.    Note: Most of the stuff done in here can be found in manpage if(4n)
  11.         If not, try if_tcp(7)
  12.  */
  13.  
  14. #define ICBUFS    (1 + 10 * sizeof(struct ifreq))    /* interface request bufs */
  15.  
  16. #include    <stdio.h>
  17. #include     <sys/types.h>
  18. #include    <sys/socket.h>
  19. #include    <sys/ioctl.h>
  20. #ifndef SIOCGIFCONF
  21. #include    <sys/sockio.h>
  22. #endif
  23. #include    <net/if.h>
  24. #include    <netinet/in.h>
  25. #include    <arpa/inet.h>
  26. #include    <stdio.h>
  27. #include    <string.h>
  28.  
  29. main ()
  30. {
  31.     int     sock,on=1,nif,i,found=0;
  32.     char    icBuf[ICBUFS];
  33.     struct    ifconf         ifc;
  34.     struct     ifreq        *ifr;
  35.     struct     sockaddr_in sin;
  36.  
  37.     /* get udp socket */
  38.     if ((sock = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
  39.         perror("socket");
  40.         exit(1);
  41.     }
  42.     /* prepare for broadcast */
  43.     if (setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&on,sizeof(int)) < 0) {
  44.         perror("setsockopt");
  45.         exit(1);
  46.     }
  47.     /* get ALL interface configs */
  48.     ifc.ifc_len = ICBUFS;
  49.     ifc.ifc_buf = icBuf;
  50.     if (ioctl(sock,SIOCGIFCONF,(char *)&ifc) < 0) {
  51.         perror("ioctl");
  52.         exit(1);
  53.     }
  54.     if (ifc.ifc_len == ICBUFS-1) 
  55.         printf("Warning: buffer full; some interfaces may not be reported!\n");
  56.     nif = ifc.ifc_len/sizeof(struct ifreq);
  57.     
  58.     /* now extract values */
  59.     for (i=0,ifr = ifc.ifc_req; i<nif; i++,ifr++) {
  60.         /* Only INET */
  61.         if (ifr->ifr_addr.sa_family != AF_INET) continue;
  62.         /* Get Flags */
  63.         if (ioctl(sock,SIOCGIFFLAGS,(char *)ifr) < 0) {
  64.             perror("ioctl");
  65.             exit(1);
  66.         }
  67.         /* Skip worthless interfaces */
  68.         if (((ifr->ifr_flags & IFF_UP) == 0) ||
  69.             ((ifr->ifr_flags & IFF_LOOPBACK) != 0) ||
  70.             ((ifr->ifr_flags &
  71.             (IFF_BROADCAST | IFF_POINTOPOINT)) == 0)) continue;
  72.         found++;
  73.         memcpy((char *)&sin,
  74.                (char *)&ifr->ifr_addr,
  75.                sizeof sin);
  76.         printf("Interface <%s>:\n",ifr->ifr_name);
  77.         printf("\tLocal Address: %s\n",
  78.             inet_ntoa(sin.sin_addr));
  79.         /* bcast interface */
  80.         if (ifr->ifr_flags & IFF_BROADCAST) {
  81.             if (ioctl(sock,SIOCGIFBRDADDR,(char *)ifr) < 0) {
  82.                 perror("ioctl");
  83.                 exit(1);
  84.             }
  85.             found++;
  86.                     memcpy((char *)&sin,
  87.                    (char *)&ifr->ifr_addr,
  88.                            sizeof sin);
  89.             printf("\tBroadcast Address: %s\n",
  90.                 inet_ntoa(sin.sin_addr));
  91.                 }
  92.     }
  93.     close(sock);
  94. }
  95.  
  96.